package clh;

import static org.junit.Assert.assertTrue;
import robocode.control.events.BattleCompletedEvent;
import robocode.control.events.TurnEndedEvent;
import robocode.control.snapshot.IRobotSnapshot;
import robocode.control.testing.RobotTestBed;

/**
 * Tests that Chuan's gun is always pointing at the enemy plus or minus 10 degrees due to the delay
 * in each turn.
 * 
 * @author Chuan Lun Hung
 * 
 */
public class TestChuanTracking extends RobotTestBed {

  /** keep track of number of errors in tracking. */
  boolean correctTracking = true;
  /** variable to store Chuan's previous gun heading for comparison. */
  double previousGunHeading = 500;

  /**
   * Specifies that SittingDuck and Chuan are to be matched up in this test case.
   * 
   * @return The comma-delimited list of robots in this match.
   */
  @Override
  public String getRobotNames() {
    return "sample.Crazy,clh.Chuan";
  }

  /**
   * This test runs for 10 round.
   * 
   * @return The number of rounds.
   */
  @Override
  public int getNumRounds() {
    return 10;
  }

  /**
   * This robot's gun should be always following the enemy.
   * 
   * @param event Info about the current state of the battle.
   */
  @Override
  public void onTurnEnded(TurnEndedEvent event) {
    IRobotSnapshot robot = event.getTurnSnapshot().getRobots()[1];
    double gunHeading = robot.getGunHeading();

    // if it's 1st turn set previousGunHeading to current get heading
    if (previousGunHeading == 500) {
      previousGunHeading = robot.getGunHeading();
    }

    double difference = gunHeading - previousGunHeading;

    if (difference < -10 || difference > 10) {
      correctTracking = false;
    }
    previousGunHeading = robot.getGunHeading();
  }

  /**
   * After the battle, check to see that we have no errors.
   * 
   * @param event Details about the completed battle.
   */
  @Override
  public void onBattleCompleted(BattleCompletedEvent event) {
    assertTrue(correctTracking);
  }
}
